JSGF.BackgroundImage = JSGF.inherit(JSGF.Object);

JSGF.BackgroundImage.prototype.init = function(image, width, height) {
	this.type = "JSGF.BackgroundImage";
	this.__tid__ = null;
	this.__interval__ = 0;
	this.visible = true;
	this.image = new Array();
	this.imageNo = 0;
	this.repeat = true;
	this.x = 0;
	this.y = 0;
	this.width = null;
	this.height = null;
	if (width != null) {
		this.width = width;
	}
	if (height != null) {
		this.height = height;
	}
	if (image != null) {
		if (typeof(image) == "string") {
			var tmpimage = new Image();
			tmpimage.src = image;
			if (tmpimage.complete == true) {
				if (this.width == null) {
					this.width = tmpimage.width;
				}
				if (this.height == null) {
					this.height = tmpimage.height;
				}
				this.onImageLoad(self);
			}
			var self = this;
			tmpimage.onload = function() {
				if (self.width == null) {
					self.width = tmpimage.width;
				}
				if (self.height == null) {
					self.height = tmpimage.height;
				}
				self.onImageLoad(self);
			}
			this.image.push(tmpimage);
		} else if (typeof(image) == "object") {
			for (var i = 0; i < image.length; i++) {
				var tmpimage = new Image();
				tmpimage.src = image[i];
				if (i == 0) {
					var self = this;
					tmpimage.onload = function() {
						if (self.width == null) {
							self.width = tmpimage.width;
						}
						if (self.height == null) {
							self.height = tmpimage.height;
						}
						self.onImageLoad(self);
					}
				}
				this.image.push(tmpimage);
			}
		} else if (image.tagName && image.tagName.toLowerCase() == "img") {
			this.image.push(image);
			this.width = image.width;
			this.height = image.height;
		}
	}
}
JSGF.BackgroundImage.prototype.additional = function(image) {
}

JSGF.BackgroundImage.prototype.getImage = function() {
	if (this.image.length != 0) {
		return this.image[this.imageNo];
	} else {
		return null;
	}
}

JSGF.BackgroundImage.prototype.animationCallback = function(self) {
	if (self.tid != null) {
		clearTimeout(self.__tid__);
	}
	self.imageNo++;
	if (self.image.length <= self.imageNo) {
		self.imageNo = 0;
	}
	self.__tid__ = setTimeout(function() {
		self.animationCallback(self);
	}, self.__interval__);
}

////////////////////////////////////////
//Public
////////////////////////////////////////

JSGF.BackgroundImage.prototype.setWidth = function(width) {
	this.width = width;
}

JSGF.BackgroundImage.prototype.setHeight = function(height) {
	this.height = height;
}

JSGF.BackgroundImage.prototype.getWidth = function() {
	return this.width;
}

JSGF.BackgroundImage.prototype.getHeight = function() {
	return this.height;
}

JSGF.BackgroundImage.prototype.setVisible = function(visible) {
	this.visible = visible;
}

JSGF.BackgroundImage.prototype.getVisible = function() {
	return this.visible;
}

JSGF.BackgroundImage.prototype.setRepeat = function(repeat) {
	this.repeat = repeat;
}

JSGF.BackgroundImage.prototype.getRepeat = function() {
	return this.repeat;
}

JSGF.BackgroundImage.prototype.setPosition = function(x, y) {
	this.x = x;
	this.y = y;
}

JSGF.BackgroundImage.prototype.getPositionX = function() {
	return this.x;
}

JSGF.BackgroundImage.prototype.getPositionY = function() {
	return this.y;
}

JSGF.BackgroundImage.prototype.startAnimation = function(interval) {
	var self = this;
	if (this.__tid__ != null) {
		clearTimeout(this.__tid__);
		this.__tid__ = null;
	}
	this.__interval__ = interval;
	this.__tid__ = setTimeout(function() {
		self.animationCallback(self);
	}, interval);
}

JSGF.BackgroundImage.prototype.stopAnimation = function() {
	clearTimeout(this.__tid__);
	if (this.__tid__ != null) {
		clearTimeout(this.__tid__);
	}
	this.__tid__ = null;
	this.__interval__ = 0;
}

JSGF.BackgroundImage.prototype.scroll = function(x, y) {
	var tmpX = this.x + x;
	var tmpY = this.y + y;
	if (this.width != null) {
		tmpX = tmpX % this.width;
	}
	if (this.height != null) {
		tmpY = tmpY % this.height;
	}
	this.setPosition(tmpX, tmpY);
}

////////////////////////////////////////
// definition by user
////////////////////////////////////////

JSGF.BackgroundImage.prototype.onLoad = function() {

}

JSGF.BackgroundImage.prototype.onUnload = function() {

}

JSGF.BackgroundImage.prototype.onImageLoad = function(sprite) {

}

